package com.gdit.zax.filter;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.VideoDao;
import com.gdit.zax.dao.VideoRecordDao;
import com.gdit.zax.entitys.VideoInfo;
import com.gdit.zax.entitys.VideoRecord;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;

public class VideoFilter implements Filter {
	// %root%/videos/test.mp4?key=a32b9a9ccd2df92e0410690273a3dfc4

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		String url = httpRequest.getRequestURI();
		String filename = TextUtil.getUrlLastName(url);
		int type = TextUtil.String2int(request.getParameter("type"), 0);//必须
		String accesstoken = request.getParameter("accesstoken");// 可选
		
		
		File file = new File(Constant.WEBPATH + File.separator + "videos" + File.separator + filename);
		if (!file.exists()) {
			LogUtil.print("视频已不存在：" + filename);
			// 验证通过,文件不存在
			Repost.success("default.mp4", httpRequest, httpResponse);
			return;
		}
		
		
		Connection conn = DBUtil.getConnection();
		if (conn==null) {
			//数据库连接失败
			Repost.success("default.mp4", httpRequest, httpResponse);
			return;
		}
		DAOFactory dao = DAOFactory.init(conn, httpRequest, httpResponse);
		VideoRecordDao vrdao = dao.getVideoRecordDao();
		VideoDao vdao = dao.getVideoDao();
		int uid = Integer.MIN_VALUE;
		if (!TextUtil.isEmpty(accesstoken)) {
			uid = VeriFicUtil.getVFID(accesstoken);
		}
		VideoInfo vinfo = vdao.getvideoinfobyname(filename);
		
		
		if (type == 0) {//html5播放器播放
			//记录播放
			if (!(uid==Integer.MIN_VALUE || uid == Integer.MAX_VALUE || vinfo==null)) {
				VideoRecord vr = new VideoRecord();
				vr.setUserId(uid);
				vr.setCourseId(vinfo.getCourseid());
				vr.setSectionid(vinfo.getSectionid());
				vr.setVideoId(vinfo.getId());
				vrdao.addseerecord(vr);
			}
		} else if (type == 1) {//app播放
			//记录播放
			if (!(uid==Integer.MIN_VALUE || uid == Integer.MAX_VALUE || vinfo==null)) {
				VideoRecord vr = new VideoRecord();
				vr.setUserId(uid);
				vr.setCourseId(vinfo.getCourseid());
				vr.setSectionid(vinfo.getSectionid());
				vr.setVideoId(vinfo.getId());
				vrdao.addseerecord(vr);
			}
		} else if (type == 2) {//app下载
			//记录下载
			if (!(uid==Integer.MIN_VALUE || uid == Integer.MAX_VALUE || vinfo==null)) {
				VideoRecord vr = new VideoRecord();
				vr.setUserId(uid);
				vr.setCourseId(vinfo.getCourseid());
				vr.setSectionid(vinfo.getSectionid());
				vr.setVideoId(vinfo.getId());
				vrdao.adddownrecord(vr);
			}
		} else {
			//未知标记
			Repost.success("default.mp4", httpRequest, httpResponse);
			return;
		}



		LogUtil.print("读取文件:" + filename);
		// 传递控制到下一个过滤器
		arg2.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		Constant.WEBPATH = filterConfig.getServletContext().getRealPath("/");
	}

	public void destroy() {

	}

}
